<?php
require_once BACKGROUND_COMMON_ACTION_SERVICE_DIR . '/default.php';

class background_index_do_login_action_service extends background_default_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
	     
	    $user_name = init_user_data_key($_POST, 'user_name', 'str');
	    $password  = init_user_data_key($_POST, 'password', 'str');
	    $admin_login_captcha  = init_user_data_key($_POST, 'admin_login_captcha', 'str');
	   
	    if(empty($user_name)){
	        $result['status']  = -400;
	        $result['message'] = '请输入帐号';
	    }
	    else if(empty($password)){
	        $result['status']  = -400;
	        $result['message'] = '请输入密码';
	    }
	    else if(empty($admin_login_captcha)){
	        $result['status']  = -400;
	        $result['message'] = '请输入验证码';
	    }
	    else if(empty($_SESSION['admin_login_captcha'])){
	        $result['status']  = -400;
	        $result['message'] = '非法操作';
	    }
	    else if(strtolower($admin_login_captcha) !== $_SESSION['admin_login_captcha']){
	        $result['status']  = -400;
	        $result['message'] = '验证码错误';
	    }	    
	    
	    // 判断帐号、密码
	    if($result['status'] >= 0){
    	    $admin_info = wwy_resource_container::get_config_item('admin_info');
    	    $user_name_verify = $admin_info['user_name'];
    	    $password_verify = $admin_info['password'];;
    	    
    	    if($user_name !== $user_name_verify){
    	        $result['status']  = -400;
    	        $result['message'] = '帐号或密码错误';
    	    }
	    }
	    
	    if($result['status'] >= 0){
    	    $password_encode = md5($password); 
    	    if($password_encode !== $password_verify){
    	        $result['status']  = -400;
    	        $result['message'] = '帐号或密码错误';
    	    }
    	    	    
    	    self::add_view_data('user_name', $user_name);
    	    
    	    $result['status']  = 1;
    	    $result['message'] = '登录成功';
	    }
	     
	    self::set_view_status($result['status']);
	    self::set_view_message($result['message']);
    }
}